package se.webbzon.boltzmann.game.object.dialogue.word;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3d;

import se.webbzon.boltzmann.game.object.dialogue.AbstractDialogue;
import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleGroupAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.XYPlaneAppearance3D;

public class ImageDialogueWord extends AbstractDialogueWord {

	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new image dialogue word by displaying the texture set
	 * with the given name. **/
	public ImageDialogueWord(DialogueLayout layout, String textureSetName) {
		this(layout,Resources.getTextureSet(textureSetName));
	}
	
	/** Creates a new image dialogue word by displaying the given texture set. **/
	public ImageDialogueWord(DialogueLayout layout, ResourceSet<Texture3D> textureSet) {
		super(layout, createAppearance(textureSet));
		final double size = layout.getCharacterWidth() * 4;
		setSize(size,size,1);
		hide();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Sets the texture set used for this image dialogue word. **/
	public void setTextureSet(ResourceSet<Texture3D> textureSet) {
		SimpleGroupAppearance3D appearances = (SimpleGroupAppearance3D) getAppearance();
		XYPlaneAppearance3D plane = (XYPlaneAppearance3D) appearances.getAppearance(0);
		plane.setTextureSet(textureSet);
	}
	
	@Override public void getDimension(Dimension d) {
		d.setSize(getWidth(), getHeight());
	}
	
	@Override public boolean isNewLine() {
		return false;
	}

	@Override public String getWord() {
		return "?";
	}

	@Override public void onUpdateWord(AbstractDialogue dialogue) {};

	@Override public void onShowCharacter(AbstractDialogue dialogue, int character) {
		if (character == 0)
			hide();
		else
			display();
	}

	@Override public void onShowAllCharacters(AbstractDialogue dialogue) {
		onShowCharacter(dialogue,1);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates the appearance of this image dialogue word. **/
	private static final Appearance3D createAppearance(ResourceSet<Texture3D> textureSet) {
		XYPlaneAppearance3D plane = 
				new XYPlaneAppearance3D(1,	SimpleAppearance3D.LAYERS + 
											SimpleAppearance3D.TRANSPARENCY, false);
		plane.setTextureSet(textureSet);
		plane.setLayer(0, 0, 1);
		
		Transform3D transform = new Transform3D();
		Vector3d translation = new Vector3d(0.5,-0.5,0);
		transform.setTranslation(translation);
		plane.setTransform(transform);
		
		ArrayList<Appearance3D> appearances = new ArrayList<Appearance3D>();
		appearances.add(plane);
		
		return new SimpleGroupAppearance3D(appearances);
	}

}
